/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.operate.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.QiniuUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.market.entity.Event;
import com.kaaction.modules.market.entity.EventChannel;
import com.kaaction.modules.market.entity.Works;
import com.kaaction.modules.market.service.EventChannelService;
import com.kaaction.modules.market.service.WorksService;
import com.kaaction.modules.operate.entity.Client;
import com.kaaction.modules.operate.entity.ClientMaterial;
import com.kaaction.modules.operate.entity.Material;
import com.kaaction.modules.operate.service.ClientMaterialService;
import com.kaaction.modules.operate.service.ClientService;
import com.kaaction.modules.operate.service.MaterialService;

/**
 * 客户Controller
 * @author zengbiao
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "/api/v1/operate/client")
public class ClientApiController extends BaseController {

	@Autowired
	private ClientService clientService;
	
	@Autowired
	private MaterialService materialService;
	
	@Autowired
	private ClientMaterialService clientMaterialService;
	
	@Autowired
	private WorksService worksService;
	
	@Autowired
	private EventChannelService eventChannelService;
	
	@RequestMapping(value = {"add"})
	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String eventId = request.getParameter("eventId");
		String channelId = request.getParameter("channelId");
		String userName = request.getParameter("userName");
		String userSex = request.getParameter("userSex");
		String userTel = request.getParameter("userTel");
		String userIdCard = request.getParameter("userIdCard");
		String userAddress = request.getParameter("userAddress");
		String userPay = request.getParameter("userPay");
		String userTime = request.getParameter("userTime");
		String userDemands = request.getParameter("userDemands");
		
		String userDemandsOher = request.getParameter("userDemandsOher");
		String ownBy = request.getParameter("signPerson");
		String title = request.getParameter("workName");
		String content = request.getParameter("productDesc");
		String qiniuImage = request.getParameter("qiniuImage");
		logger.info("userName="+userName+",userSex="+userSex);
		
		Client client = new Client();
		client.setAddress(userAddress);
		client.setChannelId(channelId);
		client.setGender(userSex);
		client.setMobile(userTel);
		client.setName(userName);
		client.setMarryScheduleDate(userTime);
		client.setUserDemands(userDemands);
		client.setUserDemandsOther(userDemandsOher);
		client.setMarryScheduleFee(userPay);
		client.setIdCard(userIdCard);
		clientService.save(client);
		
		if(!StringUtils.isBlank(ownBy) && !StringUtils.isBlank(title) && !StringUtils.isBlank(content)){
			Works work = new Works();
			work.setClient(client);
			work.setOwnBy(ownBy);
			work.setTitle(title);
			work.setContent(content);
			work.setWorks(QiniuUtils.getCloudDomain() + qiniuImage);
			work.setSourceType("market");
			
			Event event = new Event();
			event.setId(eventId);
			work.setEvent(event);
			worksService.save(work);

		}
		
		if(!StringUtils.isBlank(userDemands)) {
			String[] commandArray = userDemands.split(",");
			for(String command : commandArray){
				if(!"".equals(command)){
					ClientMaterial clientMaterial = new ClientMaterial();
					clientMaterial.setClientId(client.getId());
					clientMaterial.setMaterialId(command);
					clientMaterialService.save(clientMaterial);
				}
			}
		}
		
		//打开页面时，记录次数
		EventChannel eventChannel = eventChannelService.getByEventAndChannel(eventId,channelId);
		int joinNum = 1 + eventChannel.getJoinNum();
		eventChannel.setJoinNum(joinNum);
		eventChannelService.save(eventChannel);
		
		
		String s = client.getId();
		System.out.println(s);
		response.setHeader("Access-Control-Allow-Origin", "*");
		PrintWriter pw = response.getWriter();
		String ss = "{'data':'3'}";
		pw.write("3");
		pw.flush();
		pw.close();
	}

}